// components/loginModal/loginModal.js
var ajax = require('../../utils/ajax.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
      observer: function (nv, ov) {
        console.log(nv);
        console.log(ov);
        this.setData({
          show: nv,
        });
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    phone: "",
    avatarUrl: "",
    nickName: "",
    avatarUpload: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleClose() {
      this.triggerEvent("close");
    },
    onChooseAvatar(e) {
      console.log(e);
      const { avatarUrl } = e.detail;
      this.setData({
        avatarUrl,
      });
    },
    getPhoneNumber(e) {
      const { code } = e.detail;
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
      const requestParam = {
        code : code
      }
      // wx.$request.post('camera/getMobile',requestParam).then(res => {
      //   wx.hideLoading()
      //   this.setData({
      //     phone : res.data
      //   })
      // })
      ajax.requestPost({
        url:'applet/user/getMobile',
        data: requestParam,
        success: res =>{
          console.log(res)
          this.setData({
            phone : res.data
          })
        }
      })
    },
    async updateUserInfo() {
      if (!this.data.avatarUrl || !this.data.nickName) {
        return wx.showToast({
          title: "请完成信息后提交",
          icon: 'none',
        });
      }
        wx.uploadFile({
          url: 'http://localhost:8848/obs/upload',
          filePath: this.data.avatarUrl,
          name:'files',
          success: res => {
            this.setData({
              avatarUpload : JSON.parse(res.data).data
            })
            if (this.data.avatarUpload) {
              const requestParam = {
                userImage: this.data.avatarUpload,
                nickName: this.data.nickName,
                phone: this.data.phone,
                userId: wx.getStorageSync('UserNo')
              };
              ajax.requestPost({
                url: "applet/user/updateUserInfo",
                data: requestParam,
                success: res=>{
                  wx.showToast({
                    title: '操作成功！',
                    icon: 'none'
                  })
    
                  this.triggerEvent("success");
                }
              })
            }
          }
        });
    },
  },
});
